package com.web.exiaodao.remote.service.impl;

import com.google.common.base.Joiner;
import com.web.exiaodao.business.constant.JConstant;
import com.web.exiaodao.business.entity.ShopStandardName;
import com.web.exiaodao.remote.vo.ShopStandardNameVo;

import java.math.BigInteger;
import java.util.*;

import com.web.exiaodao.remote.vo.ShopStandardVo;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.web.exiaodao.business.repository.ShopStandardNameRepository;
import com.web.exiaodao.remote.service.ApiShopStandardNameService;
import com.web.webstart.base.constant.XaConstant;
import com.web.webstart.base.exception.BusinessException;
import com.web.webstart.base.util.jpa.DynamicSpecifications;
import com.web.webstart.base.util.jpa.SearchFilter;
import com.web.webstart.base.service.impl.BaseService;
import com.web.webstart.base.util.jpa.SearchFilter.Operator;
import com.web.webstart.base.util.result.XaResult;
import com.web.webstart.base.util.XaUtil;

/**
 * @author: zhangl
 * @times: 2016-11-14 11:27:38
 * 类的说明：前端APIShopStandardName接口实现类
 **/
@Service("ApiShopStandardNameService")
@Transactional(readOnly = false)
public class ApiShopStandardNameServiceImpl extends BaseService<ShopStandardName> implements ApiShopStandardNameService{

	private static final Logger LOGGER = Logger.getLogger(ApiShopStandardNameServiceImpl.class);

	@Autowired
	ShopStandardNameRepository shopStandardNameRepository;

	@Override
	public XaResult<ShopStandardNameVo> findOne(Long tId) throws BusinessException {
		throw new BusinessException(XaConstant.Message.error_method_no_impl);
	}

	@Override
	public XaResult<List<ShopStandardNameVo>> findListNEStatusByFilter(
			Integer status, Map<String, Object> filterParams, Pageable pageable)
			throws BusinessException {
		Map<String, SearchFilter> filters = SearchFilter.parse(filterParams);
		if(status == null){// 默认显示非删除的所有数据
			status = XaConstant.Status.delete;
		}
		filters.put("status", new SearchFilter("status", Operator.NE, status));
		Page<ShopStandardName> page = shopStandardNameRepository.findAll(DynamicSpecifications
				.bySearchFilter(filters.values(), ShopStandardName.class), pageable);
		XaResult<List<ShopStandardNameVo>> xr = new XaResult<List<ShopStandardNameVo>>();
		xr.setObject(JSON.parseArray(JSON.toJSONString(page.getContent()), ShopStandardNameVo.class));
		for(int i=0;i<page.getContent().size();i++){
			xr.getObject().get(i).setId(page.getContent().get(i).getId());
		}
		return xr;
	}

	@Override
	public XaResult<List<ShopStandardNameVo>> findListEQStatusByFilter(
			Integer status, Map<String, Object> filterParams, Pageable pageable)
			throws BusinessException {
		Map<String, SearchFilter> filters = SearchFilter.parse(filterParams);
		if(status == null){// 默认显示正常数据
			status = XaConstant.Status.valid;
		}
		filters.put("status", new SearchFilter("status", Operator.EQ, status));
		Page<ShopStandardName> page = shopStandardNameRepository.findAll(DynamicSpecifications
				.bySearchFilter(filters.values(), ShopStandardName.class), pageable);
		XaResult<List<ShopStandardNameVo>> xr = new XaResult<List<ShopStandardNameVo>>();
		xr.setObject(JSON.parseArray(JSON.toJSONString(page.getContent()), ShopStandardNameVo.class));
		for(int i=0;i<page.getContent().size();i++){
			xr.getObject().get(i).setId(page.getContent().get(i).getId());
		}
		return xr;
	}

	@Override
	public XaResult<ShopStandardNameVo> multiOperate(String modelIds,
			Integer status) throws BusinessException {
		throw new BusinessException(XaConstant.Message.error_method_no_impl);
	}

	@Override
	public XaResult<ShopStandardNameVo> createModel(ShopStandardName model)
			throws BusinessException {
		XaResult<ShopStandardNameVo> xr = new XaResult<ShopStandardNameVo>();
		ShopStandardName obj = shopStandardNameRepository.save(model);
		xr.setObject(JSON.parseObject(JSON.toJSONString(obj), ShopStandardNameVo.class));
		xr.getObject().setId(obj.getId());
		LOGGER.info("新增数据成功----->ShopStandardName");
		return xr;
	}

	@Override
	public XaResult<ShopStandardNameVo> updateModel(ShopStandardName model)
			throws BusinessException {
		ShopStandardName obj = shopStandardNameRepository.findOne(model.getId());
		XaResult<ShopStandardNameVo> xr = new XaResult<ShopStandardNameVo>();
		if (XaUtil.isNotEmpty(obj)) {
					obj.setShopId(XaUtil.isNotEmpty(model.getShopId()) ? model.getShopId() : obj.getShopId());
					obj.setShopStandsName(XaUtil.isNotEmpty(model.getShopStandsName()) ? model.getShopStandsName() : obj.getShopStandsName());
				obj = shopStandardNameRepository.save(obj);
			xr.setObject(JSON.parseObject(JSON.toJSONString(obj), ShopStandardNameVo.class));
			xr.getObject().setId(obj.getId());
		} else {
			xr.error(XaConstant.Message.object_not_find);
		}
		LOGGER.info("编辑数据成功----->ShopStandardName");
		return xr;
	}

	@Override
	public XaResult<List<ShopStandardVo>> findShopStandsByValueIds(Set<String> shopStandValueIds) throws BusinessException {
	    XaResult<List<ShopStandardVo>> xr = new XaResult<>();
		if (XaUtil.isEmpty(shopStandValueIds)) {
			return xr;
		}
        //查询所有的规格
        StringBuffer standardsSql = new StringBuffer();	//规格查询语句
        standardsSql.append("SELECT valueId, shop_id, nameId, shop_stands_name, shop_stands_value FROM view_shop_standards  WHERE valueId in ( "+ Joiner.on(JConstant.SYMBOL_DOT).join(shopStandValueIds) +" ) ");
        List<Object[]> stands = this.query(standardsSql.toString(), 0,  Integer.MAX_VALUE);
        //数据结果集
        List<ShopStandardVo> shopStandardVos = new ArrayList<>();
        for (Object[] stand : stands) {
            ShopStandardVo vo = new ShopStandardVo();
            vo.setValueId(((BigInteger)stand[0]).longValue());
            vo.setShop_id(((BigInteger)stand[1]).longValue());
            vo.setNameId(((BigInteger)stand[2]).longValue());
            vo.setShop_stands_name((String) stand[3]);
            vo.setShop_stands_value((String) stand[4]);
            shopStandardVos.add(vo);
        }
        xr.setObject(shopStandardVos);
		return xr;
	}

	@Override
	public XaResult<List<ShopStandardVo>> findShopStandsByShopIds(Set shopIds) throws BusinessException {
		XaResult<List<ShopStandardVo>> xr = new XaResult<>();
		//查询所有的规格
		StringBuffer standardsSql = new StringBuffer();	//规格查询语句
		standardsSql.append("SELECT valueId, shop_id, nameId, shop_stands_name, shop_stands_value FROM view_shop_standards  WHERE shop_id in ( "+ Joiner.on(JConstant.SYMBOL_DOT).join(shopIds) +" ) ");
		List<Object[]> stands = this.query(standardsSql.toString(), 0,  Integer.MAX_VALUE);
		//数据结果集
		List<ShopStandardVo> shopStandardVos = new ArrayList<>();
		for (Object[] stand : stands) {
			ShopStandardVo vo = new ShopStandardVo();
			vo.setValueId(((BigInteger)stand[0]).longValue());
			vo.setShop_id(((BigInteger)stand[1]).longValue());
			vo.setNameId(((BigInteger)stand[2]).longValue());
			vo.setShop_stands_name((String) stand[3]);
			vo.setShop_stands_value((String) stand[4]);
			shopStandardVos.add(vo);
		}
		xr.setObject(shopStandardVos);
		return xr;
	}
}
